<?php
	class hoadon{
		public $MaHoaDon;
		public $TenDangNhap;
		public $idPhuongThucThanhToan;
		public $NgayDatHang;
		public $TenKH;
		public $EmailKH;
		public $DienThoaiKH;
		public $DiaChiKH;
		public $TrangThai;
		
		public function getHoaDonByWhere($where,$having ,$order){
			
			$db = new db();
			$db->getInstance();
			$sql = "SELECT h.* , sum(c.Gia * c.SoLuong) GiaSP, sum(c.Gia * c.SoLuong) + sum(c.Gia * c.SoLuong)*p.PhiDichVu/100 GiaHD , p.MoTa , NgayDatHang 
from tblhoadon h INNER JOIN tblchitiethoadon c on h.id = c.idHoaDon INNER JOIN tblphuongthucthanhtoan p on h.idPhuongThucThanhToan = p.id";
			if($where != '') $sql .= ' where ' . $where; 
			$sql .= " group by MaHoaDon";
			if($having != '') $sql .= ' having ' . $having;
			if($order != '') $sql .= ' order by ' . $order;
			$rs = mysql_query($sql);//echo $sql; die;
			if(mysql_num_rows($rs)>0){ 
				while($rc = mysql_fetch_object($rs)){
					$lstHangSanXuat[] = $rc;	
				}
				
				return $lstHangSanXuat;
			}
			else
				return null;
		}
		
		public function getHoaDonByWherePaging($where,$having, $order, $pos){
			
			$db = new db();
			$db->getInstance();
			$sql = "SELECT h.* , sum(c.Gia * c.SoLuong) GiaSP, sum(c.Gia * c.SoLuong) + sum(c.Gia * c.SoLuong)*p.PhiDichVu/100 GiaHD , p.MoTa 
from tblhoadon h INNER JOIN tblchitiethoadon c on h.id = c.idHoaDon INNER JOIN tblphuongthucthanhtoan p on h.idPhuongThucThanhToan = p.id";
			if($where != '') $sql .= ' where ' . $where; 
			$sql .= " group by MaHoaDon";
			if($having != '') $sql .= ' having ' . $having;
			if($order != '') $sql .= ' order by ' . $order;
			
			$sql .= ' limit '.$pos.','.__PAGE_SIZE; //echo $sql;die;
			$rs = mysql_query($sql);
			if(mysql_num_rows($rs)>0){
				while($rc = mysql_fetch_object($rs)){
					$lstHoaDon[] = $rc;	
				}
				return $lstHoaDon;
			}
			else
				return null;
		}
		
		public function getHoaDonById($id){
			$db = new db();
			$db->getInstance();
			$sql = "SELECT h.* , sum(c.Gia * c.SoLuong) GiaSP, sum(c.Gia * c.SoLuong) + sum(c.Gia * c.SoLuong)*p.PhiDichVu/100 GiaHD , p.MoTa ,sum(c.Gia * c.SoLuong)*p.PhiDichVu/100 PhiDV 
from tblhoadon h INNER JOIN tblchitiethoadon c on h.id = c.idHoaDon INNER JOIN tblphuongthucthanhtoan p on h.idPhuongThucThanhToan = p.id where h.id = " . $id;
			$rs = mysql_query($sql);
			$rc = mysql_fetch_object($rs);
			return $rc;
		}
		
		public function getChiTietHoaDonByIdHoaDon($hoadon_id){
			$db = new db();
			$db->getInstance();
			$sql = "Select * from tblchitiethoadon where idHoaDon = " . $hoadon_id;
			$rs = mysql_query($sql);
			if(mysql_num_rows($rs)>0){
				while($rc = mysql_fetch_object($rs)){
					$lstChiTiet[] = $rc; 
				}
				return $lstChiTiet;
			}
			
			return $null;
		}
		
		public function insertHoaDon($hoadon){
			$db = new db();
			$db->getInstance();
			$sql = "Insert tblhoadon(MaHoaDon,TenDangNhap,idPhuongThucThanhToan,NgayDatHang,TenKH,EmailKH,DienThoaiKH,DiaChiKH,TrangThai) values('".$hoadon->MaHoaDon."','".$hoadon->TenDangNhap."',".$hoadon->idPhuongThucThanhToan.",'".$hoadon->NgayDatHang."','".$hoadon->TenKH."','".$hoadon->EmailKH."','".$hoadon->DienThoaiKH."','".$hoadon->DiaChiKH."',".$hoadon->TrangThai.")"; 
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;
		}
		
		public function getMaxHoaDonId(){
			$db = new db();
			$db->getInstance();
			$sql = "Select Max(id) as idmax from tblhoadon ";
			$rs = mysql_query($sql);
			$rc = mysql_fetch_object($rs);
			return $rc->idmax;	
		}
		
		//tao moi ma hoa don co dang shoptivi + idhoadon (idhoadon co 5 chu so)
		public function setMaHoaDon(){
			$id = hoadon::getMaxHoaDonId() + 1;
			if($id<10) return 'shoptivi'.'0000'.$id;
			if($id>=10 && $id <100) return 'shoptivi'.'000'.$id;
			if($id>=100 && $id <1000) return 'shoptivi'.'00'.$id;
			if($id>=1000 && $id <10000) return 'shoptivi'.'0'.$id;
			if($id>10000) return 'shoptivi'.$id;	
		}
		
		public function insertChiTietHoaDon($idhoadon,$idtivi,$gia,$soluong){
			$db = new db();
			$db->getInstance();
			$sql = "Insert tblchitiethoadon(idHoaDon,idTivi,Gia,SoLuong) values(".$idhoadon.",".$idtivi.",'".$gia."',".$soluong.")"; 
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;
		}
		
		public function getChiTietHoaDonByWhere($where,$order)
		{
			$db = new db();
			$db->getInstance();
			$sql = "select * from tblchitiethoadon";
			if($where != '') $sql .= ' where ' . $where;
			if($order != '') $sql .= ' order by' . $order;
			$rs = mysql_query($sql);
			while($rc = mysql_fetch_object($rs)){
				$lstHangSanXuat[] = $rc;	
			}
			return $lstHangSanXuat;
		}
		
		public function updateHoaDon($set,$id)
		{
			$db = new db();
			$db->getInstance();
			$sql = "update tblHoaDon set ".$set." where id = ".$id ;
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function deleteHoaDon($hoadon_id){
			$db = new db();
			$db->getInstance();
			$sql = "delete from tblHoaDon where id = ".$hoadon_id ;
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function getBestSell(){
			$db = new db();
			$db->getInstance();
			$sql = "select idTivi, sum(c.SoLuong) sum , TenTivi, Images, Images2,Images3,Images4,Images5, GiaBan,GiaThat, TenHang
from tblchitiethoadon c INNER JOIN tbltivi t on c.idTivi = t.id INNER JOIN tblhangsanxuat h ON h.id = t.idHangSanXuat
where idHoaDon in (SELECT id from tblhoadon where TrangThai = 2 or TrangThai = 1) GROUP BY idTivi ORDER BY sum desc" ;
			$rs = mysql_query($sql);
			while($rc = mysql_fetch_object($rs)){
				$lstHangSanXuat[] = $rc;	
			}
			return $lstHangSanXuat;	
		}
		
		
	}
?>